package com.zhuhjay.chat.client.handler;

import com.zhuhjay.chat.client.manager.RpcServicePromiseManager;
import com.zhuhjay.chat.message.RpcResponseMessage;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.concurrent.Promise;
import lombok.extern.slf4j.Slf4j;

/**
 * @author ZhuHJay
 * @date 2023/3/10 21:54
 */
@Slf4j
@ChannelHandler.Sharable
public class RpcResponseMessageHandler extends AbstractResponseMessageHandler<RpcResponseMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, RpcResponseMessage msg) throws Exception {
        int sequenceId = msg.getSequenceId();
        Promise<Object> promise = RpcServicePromiseManager.remove(sequenceId);
        if (promise == null) {
            return;
        }
        if (msg.getExceptionValue() == null) {
            promise.setSuccess(msg.getReturnValue());
        } else {
            promise.setFailure(msg.getExceptionValue());
        }
    }
}
